DECLARE SUB Trippy ()
DECLARE SUB DissolveScreen ()
DECLARE SUB HitAnyKey ()
DECLARE SUB Dead ()
DECLARE SUB RotatePalette (n$)
DECLARE SUB RunX ()
DECLARE SUB RunY ()
DECLARE SUB SetPalette ()
DECLARE SUB SetPts ()
'$INCLUDE: 'c:\bc7\programs\yoshi\global.bi'

DEFINT A-Z
SUB RotatePalette (n$)
   '
   STATIC cngdiv, cng         'Keep values of these two between calls
   IF cngdiv = 0 THEN cngdiv = 1 - INT(RND * 2) * 2 'Random direction
   cng = cng - cngdiv
   IF cng < 1 THEN cng = 192
   IF cng > 192 THEN cng = 1
   s = (1 - SGN(cngdiv)) / 2  's=0 or s=1
   '
   IF n$ = " " THEN n$ = "": cngdiv = -cngdiv 'Change direction
   '
   IF cng <> 1 + s * 191 THEN
      FOR lp = 1 TO 3
         pal(cng, lp) = pal(cng - cngdiv, lp)
      NEXT lp
   ELSE
      FOR lp = 1 TO 3
         pal(1 + s * 191, lp) = pal(192 - s * 191, lp)
      NEXT lp
   END IF
   '
   OUT &H3C8, cng                  'Write Palette Register
   OUT &H3C9, pal(cng, 1)          'Write Palette Data(RGB)
   OUT &H3C9, pal(cng, 2)
   OUT &H3C9, pal(cng, 3)
   '
END SUB

SUB RunX
   
   R = INT(RND * 10)                   ' These two lines plus 'STEP 10'
   FOR X = 0 TO 9                      ' For the luxaflex-effect
      FOR yy = 0 TO 199 STEP 10
         y = yy + (X + R) MOD 10                'This one too :)
         ch = POINT(319, y): cl = POINT(0, y)
         cdiv! = (ch - cl) / 319
         FOR i = 1 TO 318
            PSET (i, y), cl + INT(i * cdiv!)    'Interpolate color
            '
            CALL RotatePalette(k$)     'these three lines
            IF k$ = CHR$(27) THEN END  'for effects during redraw
            IF k$ <> "" THEN stp = 1
            '
         NEXT i
         IF stp = 1 THEN LINE (0, 0)-(319, 199), 0, B: EXIT SUB
      NEXT yy
   NEXT X
   LINE (0, 0)-(319, 199), 0, B
   
END SUB

SUB RunY
   
   FOR i = 0 TO 1
      ch = POINT(i * 319, 199): cl = POINT(i * 319, 0)
      FOR j = 1 TO 198
         PSET (i * 319, j), cl + INT((j / 199) * (ch - cl))
      NEXT j
   NEXT i
   
END SUB

SUB SetPalette
  
   RA = 1: GA = 0: BA = 0: Rval = 63
   FOR cnt = 1 TO 192
      Rval = Rval + RA
      Gval = Gval + GA
      Bval = Bval + BA
      
      IF Rval > 62 AND Gval <> 63 AND Bval = 0 THEN GA = 2
      IF Gval > 62 AND Bval <> 63 AND Rval = 0 THEN BA = 2
      IF Bval > 62 AND Rval <> 63 AND Gval = 0 THEN RA = 2
      
      IF Gval > 62 AND Rval > 62 THEN RA = -2
      IF Rval < 0 AND Gval > 62 THEN BA = 2
      IF Gval > 62 AND Bval > 62 THEN GA = -2
      IF Gval < 0 AND Bval > 62 THEN RA = 2
      IF Rval > 62 AND Bval > 62 THEN BA = -2
      IF Bval < 0 AND Rval > 62 THEN GA = 2
      
      IF Rval > 62 THEN Rval = 63
      IF Rval < 0 THEN Rval = 0
      IF Gval > 62 THEN Gval = 63
      IF Gval < 0 THEN Gval = 0
      IF Bval > 62 THEN Bval = 63
      IF Bval < 0 THEN Bval = 0
      pal(cnt, 1) = Rval
      pal(cnt, 2) = Gval
      pal(cnt, 3) = Bval
      OUT &H3C8, cnt
      OUT &H3C9, Rval
      OUT &H3C9, Gval
      OUT &H3C9, Bval
   NEXT cnt
   
END SUB

SUB SetPts
   
   FOR X = 0 TO 1
      PSET (X * 319, 0), (RND * 191) + 2
      PSET (X * 319, 199), (RND * 191) + 2
   NEXT X
   
END SUB

DEFSNG A-Z
SUB Trippy
DEFINT A-Z

CLS
RANDOMIZE TIMER

SCREEN 13
SetPalette
CLS
DO
   CALL SetPts
   CALL RunY
   CALL RunX
   DO
      k$ = INKEY$
      CALL RotatePalette(k$)
   LOOP UNTIL k$ <> ""
   IF k$ = CHR$(27) THEN GOTO EndOfSubroutine
LOOP

EndOfSubroutine:

  SCREEN 0, 0, 0
  WIDTH 80
  LOCATE , , 0
  PALETTE 0, 8
  COLOR 7, 0, 8
  PRINT
  PRINT "You spend the rest of your miserable life sitting on street corners and begging"
  PRINT "for food.  But, hey, you've got a good supply of those magic mushrooms.  You've"
  PRINT "also got your good companion, Harvey.  Harvey's an invisible bunny rabbit."
  PRINT "Don't ask."
  HitAnyKey
  PRINT
  PRINT
  PRINT "That's pretty much the end of this game."
  HitAnyKey
  Dead

END SUB

